import 'package:flutter/material.dart';

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _LoginPageState();
  }
}

class _LoginPageState extends State<LoginPage> {
  GlobalKey<FormState> _loginKey = new GlobalKey<FormState>();
  String userName;
  String password;

  @override
  Widget build(BuildContext context) {
    final _title = "表单的各种演示";
    return new MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: new Text(_title)),
        body: new Column(
          children: <Widget>[
            new Container(
              padding: const EdgeInsets.all(16.0),
              child: new Form(
                  key: _loginKey,
                  child: new Column(
                    children: <Widget>[
                      new TextFormField(
                        decoration: new InputDecoration(labelText: "请输入用户名"),
                        onSaved: (value) {
                          userName = value;
                        },
                        onFieldSubmitted: (value) {},
                      ),
                      new TextFormField(
                        decoration: new InputDecoration(labelText: "请输入密码"),
                        obscureText: true,
                        validator: (value) {
                          return value.length < 6 ? "密码长度不够" : null;
                        },
                        onSaved: (value) {
                          password = value;
                        },
                      )
                    ],
                  )),
            ),
            new SizedBox(
              width: 340.0,
              height: 42,
              child: new RaisedButton(
                onPressed: _login,
                child: new Text(
                  "登录",
                  style: TextStyle(fontSize: 18.0),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  void _login() {
    var loginForm = _loginKey.currentState;
    if (loginForm.validate()) {
      loginForm.save();
      print("userName  : $userName  ,  password   :  $password");
    }
  }
}
